//createWebHashHistory
import { createRouter, createWebHashHistory } from "vue-router";
import { getToken } from "@/utils/permission";

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      title: "12345热线体育类工单",
    },
  },
  {
    path: "/forgetPwd",
    name: "forgetPwd",
    component: () => import("@/pages/forgetPwd/index.vue"),
    meta: {
      title: "忘记密码",
    },
  },
  {
    path: "/",
    name: "home",
    component: () => import("@/pages/form/index.vue"),
    meta: {
      title: "首页",
      keepAlive: true,
      registerAuth: true,
    },
  },

  {
    path: "/formCheck",
    name: "formCheck",
    component: () => import("@/pages/form/formCheck/index.vue"),
    meta: {
      title: "审核工单",
      registerAuth: true,
    },
  },

  {
    path: "/formManage",
    name: "formManage",
    component: () => import("@/pages/form/formManage/index.vue"),
    meta: {
      title: "处理工单",
      registerAuth: true,
    },
  },
  {
    path: "/formDetail",
    name: "formDetail",
    component: () => import("@/pages/form/formDetail/index.vue"),
    meta: {
      title: "查看工单",
      registerAuth: true,
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 全局路由
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  if (to.meta.title) {
    document.title = `${to.meta.title}`;
  }
  const token = getToken(); // token
  if (to.meta.registerAuth) {
    if (token) {
      // 已登录，有token
      if (to.path === "/login") {
        next({
          path: "/",
        });
      } else {
        next();
      }
    } else {
      next({
        path: "/login",
      });
    }
  } else {
    next();
  }
});

// const token = getToken(); // token
// // // const redirectUrl = encodeURIComponent(`${window.location.origin}`); // 重定向url
// // const redirectUrl = "http://tiyuju.adinnet.cn/login";
// // const appId = "wwf759278f87d2de49"; // 企业微信ID
// // const scope = "snsapi_base"; // 授权类型，snsapi_base(静默授权)或snsapi_privateinfo（手动授权）
// // const urlState = "2878260"; // 用于防止CSRF攻击，可以是任意字符串
// // const agentid = "1000021"; // 企业内应用id

// // const openUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUrl}&response_type=code&scope=${scope}&state=${urlState}&agentid=${agentid}#web_login`;
// // // https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=${appId}&agentid=1000021&redirect_uri=http://tiyuju.adinnet.cn/login&state=web_login@gyoss9
// // console.log("授权链接为:",openUrl);
// if (to.meta.registerAuth) {
//   // debugger
//   if (token) {
//     //   // 已登录，有token
//       if (to.path === "/login") {
//         next({
//           path: "/",
//         });
//       } else {
//         next();
//       }
//     } else {
//       next('/login')
//     }
// } else {
//   next();
// }
// });

// router.afterEach((to, from)=>{
//   // console.log(to, from)
//   console.log('afterEach')
// })

export default router;
